package com.ndp.fb.walle.mqconsumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by infi.he on 2015/6/1.
 */
public class AllConsumerHolder {
    private static Logger logger = LoggerFactory.getLogger(AllConsumerHolder.class);

    private static LinkedBlockingDeque<AbstractPullConsumer> holder = new LinkedBlockingDeque();
    private static AtomicBoolean isDestroyed = new AtomicBoolean(false);

    public static void registryConsumer(AbstractPullConsumer abstractPullConsumer) {
        logger.info("注册服务>>>>" + abstractPullConsumer.getTopicName() + ">>> " + abstractPullConsumer.getClass());
        holder.add(abstractPullConsumer);
    }

    public static void destroyAll() {
        if (isDestroyed.compareAndSet(false, true)) {
            for (AbstractPullConsumer abstractPullConsumer : holder) {
                try {
                    logger.info("destroy >>>>" + abstractPullConsumer.getTopicName() + ">>> " + abstractPullConsumer.getClass());
                    abstractPullConsumer.destroy();
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("destroy >>>>" + abstractPullConsumer.getTopicName() + ">>> " + abstractPullConsumer.getClass(), e);
                }
            }
        }
    }
}
